package com.company.planner.web.screens.ikanalysisrule;

import com.company.planner.entity.ActiveEnum;
import com.company.planner.entity.IkAnalysisRule;
import com.haulmont.cuba.core.global.DataManager;
import com.haulmont.cuba.gui.Dialogs;
import com.haulmont.cuba.gui.ScreenBuilders;
import com.haulmont.cuba.gui.components.Button;
import com.haulmont.cuba.gui.components.DialogAction;
import com.haulmont.cuba.gui.components.GroupTable;
import com.haulmont.cuba.gui.screen.*;

import javax.inject.Inject;
import java.util.Set;

@UiController("planner_IkAnalysisRule.browse")
@UiDescriptor("ik-analysis-rule-browse.xml")
@LookupComponent("ikAnalysisRulesTable")
@LoadDataBeforeShow
public class IkAnalysisRuleBrowse extends StandardLookup<IkAnalysisRule> {
    @Inject
    private Dialogs dialogs;
    @Inject
    private GroupTable<IkAnalysisRule> ikAnalysisRulesTable;
    @Inject
    private DataManager dataManager;
    @Inject
    private ScreenBuilders screenBuilders;

    @Subscribe("disableBtn")
    public void onDisableBtnClick(Button.ClickEvent event) {
        Set<IkAnalysisRule> ikAnalysisRules = ikAnalysisRulesTable.getSelected();
        dialogs.createOptionDialog()
                .withCaption("禁用规则")
                .withMessage("确认禁用该规则？")
                .withType(Dialogs.MessageType.CONFIRMATION)
                .withActions(
                        new DialogAction(DialogAction.Type.OK)
                                .withHandler(e -> {
                                    if (!ikAnalysisRules.isEmpty()) {
                                        for (IkAnalysisRule analysisRule : ikAnalysisRules) {
                                            analysisRule.setActive(ActiveEnum.NO);
                                            dataManager.commit(analysisRule);
                                        }
                                    }
                                }),
                        new DialogAction(DialogAction.Type.CANCEL))
                .show();
    }

    @Subscribe("viewBtn")
    public void onViewBtnClick(Button.ClickEvent event) {
        IkAnalysisRule singleSelected = ikAnalysisRulesTable.getSingleSelected();
        if (singleSelected != null) {
            screenBuilders.editor(ikAnalysisRulesTable)
                    .editEntity(singleSelected)
                    .withScreenId("planner_IkAnalysisRule.query")
                    .build()
                    .show();
        }
    }

    @Subscribe("enableBtn")
    public void onEnableBtnClick(Button.ClickEvent event) {
        Set<IkAnalysisRule> ikAnalysisRules = ikAnalysisRulesTable.getSelected();
        dialogs.createOptionDialog()
                .withCaption("启用规则")
                .withMessage("确认启用该规则？")
                .withType(Dialogs.MessageType.CONFIRMATION)
                .withActions(
                        new DialogAction(DialogAction.Type.OK)
                                .withHandler(e -> {
                                    if (!ikAnalysisRules.isEmpty()) {
                                        for (IkAnalysisRule analysisRule : ikAnalysisRules) {
                                            analysisRule.setActive(ActiveEnum.YES);
                                            dataManager.commit(analysisRule);
                                        }
                                    }
                                }),
                        new DialogAction(DialogAction.Type.CANCEL))
                .show();
    }
}